/*

highlight v2

Highlights arbitrary terms.

<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>

MIT license.

Johann Burkard
<http://johannburkard.de>
<mailto:jb@eaio.com>

*/

$(function() {
    jQuery.highlight = document.body.createTextRange ?

    /*
Version for IE using TextRanges.
*/
    function(node, te) {
        var r = document.body.createTextRange();
        r.moveToElementText(node);
        for (var i = 0; r.findText(te); i++) {
            r.pasteHTML('<span class="highlight">' + r.text + '<\/span>');
            r.collapse(false);
        }
    }

    :

    /*
 (Complicated) version for Mozilla and Opera using span tags.
*/
    function(node, te) {
        var pos,
        skip,
        spannode,
        middlebit,
        endbit,
        middleclone;
        skip = 0;
        if (node.nodeType == 3) {
            pos = node.data.toUpperCase().indexOf(te);
            if (pos >= 0) {
                spannode = document.createElement('span');
                spannode.className = 'highlight';
                middlebit = node.splitText(pos);
                endbit = middlebit.splitText(te.length);
                middleclone = middlebit.cloneNode(true);
                spannode.appendChild(middleclone);
                middlebit.parentNode.replaceChild(spannode, middlebit);
                skip = 1;
            }
        }
        else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
            for (var i = 0; i < node.childNodes.length; ++i) {
                i += $.highlight(node.childNodes[i], te);
            }
        }
        return skip;
    }

    ;
});

jQuery.fn.removeHighlight = function() {
    this.find("span.highlight").each(function() {
        with(this.parentNode) {
            replaceChild(this.firstChild, this);
            normalize();
        }
    });
    return this;
};